home comics writing pictures archive about

WindowViewBase.cs

Language: C#
Last Modified: 2020-06-27 1:58:34 PM UTC
File Size: 4629 bytes
http://www.penguinstew.ca/example/MVVMbase/ViewBase/WindowViewBase.cs
using System;
using System.Windows;
using Penguin.MVVMBase.ViewModelBase;
namespace Penguin.MVVMBase.ViewBase
{
/// <summary>
/// The base class for Window views
/// </summary>
public abstract class WindowViewBase : Window
{
#region Variables
/// <summary>
/// ViewModel for this view
/// </summary>
protected IWindowViewModel m_viewModel;
#endregion
#region Constructor
/// <summary>
/// Parameterless constructor for design view
/// </summary>
public WindowViewBase() : this(new WindowViewModelBase()) { }
/// <summary>
/// Base constructor, takes viewModel as an argument and sets up event handlers
/// </summary>
/// <param name="viewModel">The viewModel for this view</param>
public WindowViewBase(IWindowViewModel viewModel)
{
m_viewModel = viewModel;
this.DataContext = m_viewModel;
//Setup event handlers
m_viewModel.ViewRequested += m_viewModel_ViewRequested;
m_viewModel.MsgBoxRequested += m_viewModel_MsgBoxRequested;
this.Loaded += View_Loaded;
}
#endregion
#region Event Handlers
/// <summary>
/// Event handler for the Loaded event
/// </summary>
private void View_Loaded(object sender, RoutedEventArgs e)
{
//Call the loaded method on the view model to deal with the view being loaded
m_viewModel.ViewLoaded(sender, e);
return;
}
/// <summary>
/// Event handler for ViewRequested, overwritten for views to handle specific viewModels
/// </summary>
/// <param name="sender">The viewModel requesting the view</param>
/// <param name="e">RequestViewArgs containing the ViewModel to use</param>
protected virtual void m_viewModel_ViewRequested(object sender, RequestViewArgs e)
{
//Default implementation, do nothing
return;
}
/// <summary>
/// Event handler for MsgBoxRequested, creates a MessageBox with given parameters
/// </summary>
/// <param name="sender">The viewModel requesting the view</param>
/// <param name="e">RequestMsgBoxArgs containing the MessageBox options</param>
protected void m_viewModel_MsgBoxRequested(object sender, RequestMsgBoxArgs e)
{
MessageBoxResult result = MessageBox.Show(e.MessageBoxText, e.MessageBoxCaption,
e.MessageBoxButtons, e.MessageBoxIcon);
if (e.Callback != null)
{
e.Callback(result);
}
}
/// <summary>
/// Handles the Closed event of the requested window view.
/// Informs the requesting viewModel of the closure.
/// </summary>
/// <param name="sender">The requested view window</param>
/// <param name="e">EventArgs</param>
private void View_Closed(object sender, EventArgs e)
{
WindowViewBase view = sender as WindowViewBase;
if(view != null)
{
//Call the requested view closed method, return is always false for windows
m_viewModel.RequestedViewClosed((IWindowViewModel)view.DataContext, false);
}
}
#endregion
#region Protected Methods
/// <summary>
/// Opens the given view.
/// </summary>
/// <param name="view"></param>
protected void OpenView(WindowViewBase view)
{
view.Owner = this;
if (view is DialogViewBase)
{
//If view is based on Dialog base open it with ShowDialog
bool? result = view.ShowDialog();
//Call requested view closed with the result, or false if no value
if (result.HasValue)
{
m_viewModel.RequestedViewClosed((IWindowViewModel)view.DataContext, result.Value);
}
else
{
m_viewModel.RequestedViewClosed((IWindowViewModel)view.DataContext, false);
}
}
else
{
//Else assume it's a window and open it with show
view.Closed += View_Closed;
view.Show();
}
}
#endregion
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134